package org.dromara.learning_xq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.learning_xq.entity.AsFormativeTest;
import org.dromara.learning_xq.entity.dto.AsFormativeTestDto;
import org.dromara.learning_xq.mapper.AsFormativeTestMapper;
import org.dromara.learning_xq.service.AsFormativeTestService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AsFormativeTestServiceimpl extends ServiceImpl<AsFormativeTestMapper, AsFormativeTest> implements AsFormativeTestService {


    @Autowired
    private AsFormativeTestMapper asFormativeTestMapper;


    /**
     * 批量增加形成性测试
     * 此方法接收一个AsFormativeTestDto列表，将其转换为实体对象列表，并批量保存到数据库中
     *
     * @param asFormativeTestDtoList 需要增加的形成性测试Dto列表
     * @return 返回增加成功的Dto列表，或在失败时返回空列表
     */
    @Override
    public List<AsFormativeTestDto> increaseAsFormativeTest(List<AsFormativeTestDto> asFormativeTestDtoList) {
        // 将Dto列表转换为实体列表
        List<AsFormativeTest> asFormativeTestList = asFormativeTestDtoList.stream()
            .map(this::convertToEntity)
            .collect(Collectors.toList());

        // 批量保存实体
        boolean result = this.saveBatch(asFormativeTestList);

        // 根据保存结果返回相应的响应
        if (result) {
            return asFormativeTestDtoList; // 或根据需要转换并返回结果
        } else {
            return List.of(); // 或根据需要处理失败情况
        }
    }


    /**
     * 将Dto对象转换为实体对象
     * 此方法负责将单个AsFormativeTestDto对象转换为AsFormativeTest实体对象，包括设置各个属性值
     *
     * @param dto 需要转换的Dto对象
     * @return 返回转换后的实体对象
     */
    private AsFormativeTest convertToEntity(AsFormativeTestDto dto) {
        AsFormativeTest entity = new AsFormativeTest();
        entity.setSubject(dto.getSubject());
        entity.setLearningStage(dto.getLearningStage());
        entity.setGrade(dto.getGrade());
        entity.setSemester(dto.getSemester());
        entity.setPaperDescription(dto.getPaperDescription());
        entity.setTestType(dto.getTestType());
        entity.setPaperName(dto.getPaperName());
        entity.setDelFlag(dto.getDelFlag());
        return entity;
    }

    @Override
    public List<AsFormativeTestDto> updateAsFormativeTest(List<AsFormativeTestDto> asFormativeTestDtoList) {
        if (asFormativeTestDtoList == null || asFormativeTestDtoList.isEmpty()) {
            return Collections.emptyList(); // 返回空列表以防止空指针异常
        }

        for (AsFormativeTestDto dto : asFormativeTestDtoList) {
            if (dto.getId() != null) {
                // 将 DTO 转换为实体类
                AsFormativeTest entity = new AsFormativeTest();
                BeanUtils.copyProperties(dto, entity);

                // 使用 MyBatis-Plus 更新操作
                asFormativeTestMapper.updateById(entity);
            }
        }

        // 返回更新后的 DTO 列表
        return asFormativeTestDtoList;
    }

    @Override
    public List<AsFormativeTestDto> selverAll(AsFormativeTestDto asFormativeTestDto) {
        QueryWrapper<AsFormativeTest> queryWrapper = new QueryWrapper<>();

        // 根据条件动态构建查询
        if (asFormativeTestDto.getSubject() != null) {
            queryWrapper.eq("subject", asFormativeTestDto.getSubject());
        }
        if (asFormativeTestDto.getLearningStage() != null) {
            queryWrapper.eq("learning_stage", asFormativeTestDto.getLearningStage());
        }
        if (asFormativeTestDto.getGrade() != null) {
            queryWrapper.eq("grade", asFormativeTestDto.getGrade());
        }
        if (asFormativeTestDto.getSemester() != null) {
            queryWrapper.eq("semester", asFormativeTestDto.getSemester());
        }

        // 查询数据库并将结果转换为 DTO 列表
        List<AsFormativeTest> entityList = asFormativeTestMapper.selectList(queryWrapper);
        return entityList.stream()
            .map(entity -> {
                AsFormativeTestDto dto = new AsFormativeTestDto();
                BeanUtils.copyProperties(entity, dto);
                return dto;
            })
            .collect(Collectors.toList());
    }

    @Override
    public Boolean removeBatchByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false; // 如果传入的ID列表为空或null，返回false
        }

        // 调用 MyBatis-Plus 的批量删除方法
        return this.removeByIds(ids);
    }
}
